/**
 * Created by Administrator on 2017/1/19 0019.
 */
if (window.location.host == 'user.kaimobangwang.com') {
    var SCOPE = {
        image_host_url : 'http://bg.kaimobangwang.com/'
    };
} else {
    var SCOPE = {
        image_host_url : 'http://mob.mgtvshop.com/'
    };
}

/**
 * 服务单状态
 */
function getOrderAustinServerStatus(status)
{
    var statusText = ['', '下单成功', '待付款', '已付款', '服务中', '待确认', '订单完成', '申请售后', '申请仲裁', '订单取消', '待确认', '议价中'];
    return statusText[status];
}

function getLocalTime(nS) {
    var time = new Date(parseInt(nS) * 1000);
    if (arguments[1]) {
        return (time.getFullYear() < 10 ? '0' + time.getFullYear() : time.getFullYear()) + "-" +
            ((parseInt(time.getMonth()) + 1) < 10 ? '0' + (parseInt(time.getMonth()) + 1) : (parseInt(time.getMonth()) + 1)) + "-" +
            (time.getDate() < 10 ? '0' + time.getDate() : time.getDate());
    } else {
        return (time.getFullYear() < 10 ? '0' + time.getFullYear() : time.getFullYear()) + "-" +
            ((parseInt(time.getMonth()) + 1) < 10 ? '0' + (parseInt(time.getMonth()) + 1) : (parseInt(time.getMonth()) + 1)) + "-" +
            (time.getDate() < 10 ? '0' + time.getDate() : time.getDate()) + " " +
            (time.getHours() < 10 ? '0' + time.getHours() : time.getHours()) + ":" +
            (time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes())+":" +
            (time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds());
    }
}

function formattingTimestamp(time) {
    if (arguments[1]) {
        return time.getFullYear() + "-" + time.getMonth()+1 + "-" + time.getDate();
    } else {
        return time.getFullYear() + "-" + time.getMonth()+1 + "-" + time.getDate() + " " + time.getHours() + ":" + time.getMinutes()+":" + time.getSeconds();
    }
}

function upload_image(url, type, l) {
    if($(".tjimg img").length > l){
        l++;
        alert('只能添加'+ l +'张图片');
    }else{
        $('#loadingToast').show();
        $('.weui-toast__content').html('请稍等...');
        var formData = new FormData();
        formData.append("type",type);
        formData.append("img",$("#uploaderInput")[0].files[0]);
        $.ajax({
            url: url,
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            dataType:"json",
            success: function (d) {
                if(!d.code){
                    alert(d.msg);
                }else{
                    if (type == 'austin') {
                        var path = SCOPE.image_host_url+d.data.path;
                        $('input[name="img'+ ($(".tjimg img").length + 1) +'"]').val(d.data.path);
                        $('#uploaderInput').parent().before('<div class="scimg'+ ($(".tjimg img").length + 1) +' tjimg"> <img src="'+ path +'" /> </div>');
                    } else {
                        var path = SCOPE.image_host_url+d.data;
                        $(".tjimg").prepend('<img src="'+path+'" onclick="this.parentNode.removeChild(this)" />');
                    }
                }
                $('#loadingToast').fadeOut();
            },
            error: function (d) {
                alert('网络异常');$('#loadingToast').fadeOut();
            }
        });
    }
}

// 上传身份证照片
function upload_id_card(_this) {
    $('#loadingToast').fadeIn();
    $('.weui-toast__content').html('正在上传...');
    var formData = new FormData();
    formData.append("type",'id_card');
    formData.append("img",_this[0].files[0]);
    $.ajax({
        url: '/wechat/base/get_upload_img',
        type: 'POST',
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        dataType:"json",
        success: function (d) {
            $('#loadingToast').fadeOut();
            if(!d.code){
                alert(d.msg);
            }else{
                d.data.path = d.data.path.replace(/\\/g,'/');
                var path = SCOPE.image_host_url+d.data.path;
                _this.next().val(d.data.path);
                _this.parents('i').css('background-image', 'url('+path+')');
            }
        },
        error: function (d) {
            alert('网络异常');$('#loadingToast').fadeOut();
        }
    });
}

/**
 * 是否微信浏览器
 * @returns {boolean}
 */
function isWeiXin(){
    var ua = window.navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i) == 'micromessenger'){
        return true;
    }else{
        return false;
    }
}

$(function () {
    /*
     * 全局js
     * */
    //ios终端
    window.isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
    //android终端
    window.isAndroid = navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Adr') > -1;
    // 上传文本框属性
    if(isiOS){
        $('input[type="file"]').attr("multiple","multiple");
    }
    if(isAndroid){
        $('input[type="file"]').attr("capture","camera");
    }

/****************************************
 *验证码下一步
 *
 * *****/


/***********************************
* 订单js
*
* */
    // 取消订单
    $(document).on('click', '.cancel_order', function () {
        if (!confirm("确认取消？")) {
            return false;
        }
        var _this = $(this);
        var id = _this.data('order-id');
        var model = _this.data('model');
        $.get("/wechat/orders/cancel_order", {id : id, model : model}, function(msg) {
            if (msg.code == 200) {
                if (_this.hasClass("order_list")) {
                    _this.text('已取消');
                    _this.parents('.js-list').find(".action p").text('已取消');
                } else {
                    _this.text('已取消').prev().remove();
                    $(".action").text('已取消');
                }
                _this.removeClass('cancel_order');
            } else {
                $.alert(error_code[msg.code]);
            }
        }, 'json');
    });

    // 重新推送订单
    $(document).on('click', '.order_push_again', function () {
        $('#loadingToast').fadeIn(100);
        //点击确认
        var _this = $(this);
        var id = _this.data('order-id');
        var model = _this.data('model');
        //如果参数过多，建议通过 object 方式传入
        $.prompt({
            title: '订单取消后将返回需求池重新推送',
            text: '请输入取消原因',
            input: '',
            empty: false, // 是否允许为空
            onOK: function (input) {
                $.get("/wechat/orders/push_again", {id : id, model : model, msg : input}, function(msg) {
                    if (msg.code == 200) {
                        alert('推送成功！！');
                        window.location.reload();
                    } else {
                        $.alert(error_code[msg.code]);
                    }
                }, 'json');
            },
            onCancel: function () {
                //点击取消
                $('#loadingToast').fadeOut(100);
                return false;
            }
        });
    });

    // 确认完成
    $(document).on('click', '.affirm_finish', function () {
        if (!confirm('确认完成订单！！')) {
            return false;
        }
        
        $('#loadingToast').fadeIn(100);
        //点击确认
        var _this = $(this);
        var id = _this.data('order-id');
        var model = _this.data('model');
        
        $.get("/wechat/orders/affirm_finish", {id : id, model : model}, function(msg) {
            $('#loadingToast').fadeOut(100);
            if (msg.code == 200) {
                $.alert('操作成功！');
                if (_this.hasClass("order_list")) {
                    _this.parents('.status').find("span").text('订单完成');
                } else {
                    $(".action").text('订单完成');
                }

                var _type = model == 'order_server' ? 2 : 1;
                _this.text('待评价').attr('href', '/wechat/w_comment/index/order_id/' + id + '/type/' + _type).removeClass('affirm_finish').next().text('申请售后');
            } else {
                $.alert(error_code[msg.code]);
            }
        }, 'json');
    });
});
